package com.example.myapplication.ui.fragment;

import android.content.Context;
import android.inputmethodservice.KeyboardView;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.myapplication.R;
import com.example.myapplication.bean.TypeBean;
import com.example.myapplication.database.AccountBean;
import com.example.myapplication.ui.adapter.TypeBaseAdapter;
import com.example.myapplication.view.BeiZhuDialog;
import com.example.myapplication.view.KeyBoardUtils;
import com.example.myapplication.view.SelectTimeDialog;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 记录页面的支出模块
 * 目前存在一个小问题，如果用户先输入信息，过一两分钟点击记账，录入的是记账时间，不是确定时间
 */

public abstract class BaseRecordFragment extends Fragment implements View.OnClickListener {
    KeyboardView keyboardView;
    private RelativeLayout fragOutRlTop;
     ImageView typeIv;
    private EditText moneyEt;
    private View fragRecrdLine1;
    private GridView fragRecordGv;
     TextView typeTv,beizhuTv,timeTv;
    List<TypeBean>typeBeanList;
    TypeBaseAdapter adapter;
    View view;
    Context context;
    AccountBean accountBean;//将需要插入到记账本当中的数据保存成对象的形式

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        accountBean=new AccountBean();//创建对象
        accountBean.setTypename("其他");
        accountBean.setsImageId(R.mipmap.ic_qita_fs);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        context=getContext();
        view=inflater.inflate(R.layout.fragment_out_come, container, false);
        initView(view);
        setInitTime();
        initData();
        setGVListener();
        return view;
    }

    /**
     * 获取当前时间
     */
    private void setInitTime() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
        String time = sdf.format(date);
        timeTv.setText(time);
        accountBean.setTime(time);
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH)+1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        accountBean.setYear(year);
        accountBean.setMonth(month);
        accountBean.setDay(day);

    }

    /**
     * 初始化控件
     * @param view
     */
    private void initView(View view) {
        fragOutRlTop = (RelativeLayout) view.findViewById(R.id.frag_out_rl_top);
        typeIv = (ImageView) view.findViewById(R.id.frag_record_iv);
        typeTv = (TextView) view.findViewById(R.id.frag_record_tv_type);
        moneyEt = (EditText) view.findViewById(R.id.frag_record_et_money);
        fragRecrdLine1 = (View) view.findViewById(R.id.frag_recrd_line1);
        fragRecordGv = (GridView) view.findViewById(R.id.frag_record_gv);
        keyboardView = (KeyboardView) view.findViewById(R.id.frag_record_keyboard);
        timeTv = (TextView) view.findViewById(R.id.frag_record_tv_time);
        beizhuTv = (TextView) view.findViewById(R.id.frag_record_tv_beizhu);
        beizhuTv.setOnClickListener(this);
        timeTv.setOnClickListener(this);
        //让自定义键盘显示
        KeyBoardUtils boardUtils = new KeyBoardUtils(keyboardView, moneyEt);
        boardUtils.shoewKeyboard();
        //设置接口,监听确定按钮被点击了
        boardUtils.setOnEnsureListener(new KeyBoardUtils.OnEnsureListener() {
            @Override
            public void onEnsure() {
                //点击了确定按钮,获取输入钱数
                String moneyStr = moneyEt.getText().toString();
                if (TextUtils.isEmpty(moneyStr)){
                    Toast.makeText(context, "输入的金额为0", Toast.LENGTH_SHORT).show();
                    return;
                }
                float money = Float.parseFloat(moneyStr);
                accountBean.setMoney(money);

                //获取记录的信息，保存在数据库当中
                saveAccountToDB();
                getActivity().finish();
                //返回上一级页面
            }
        });
    }

    /**
     * 让子类重写
     */
    public abstract void saveAccountToDB();

    /**
     * 初始化给GridView数据
     */
    public void initData(){
        typeBeanList=new ArrayList<>();
        adapter= new TypeBaseAdapter(context, typeBeanList);
        fragRecordGv.setAdapter(adapter);


    }
    //设置FridView的每一项的点击事件
    public void setGVListener(){
        fragRecordGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapter.selectPos=position;
                adapter.notifyDataSetChanged();//点击了对应图标发生变化
                TypeBean typeBean = typeBeanList.get(position);
                String typename = typeBean.getTypename();
                typeTv.setText(typename);
                int simageId = typeBean.getSimageId();
                typeIv.setImageResource(simageId);
                accountBean.setTypename(typename);
                accountBean.setsImageId(simageId);

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.frag_record_tv_time:
                showTimeDialog();
                break;
            case R.id.frag_record_tv_beizhu:
                shoewBZDialog();
                break;
        }
    }

    /**
     * 弹出显示时间对话框
     */
    private void showTimeDialog() {
        SelectTimeDialog timeDialog = new SelectTimeDialog(context);
        timeDialog.show();
        //设定确定按钮被点击了的监听器
        timeDialog.setOnEnsureListener(new SelectTimeDialog.OnEnsureListener() {
            @Override
            public void onEnsure(String time, int year, int month, int day) {
                timeTv.setText(time);
                accountBean.setTime(time);
                accountBean.setYear(year);
                accountBean.setMonth(month);
                accountBean.setDay(day);
            }
        });
    }

    /**
     * 弹出备注对话框
     */
    public void shoewBZDialog() {
        BeiZhuDialog d = new BeiZhuDialog(getContext());
        d.setDialogSize();
        d.show();
        d.setOnEnsureListener(new BeiZhuDialog.OnEnsureListener() {
            @Override
            public void onEsure() {
                String msg = d.getEditText();
                if (!TextUtils.isEmpty(msg)){
                    beizhuTv.setText(msg);
                    accountBean.setBeizhu(msg);
                }
                d.cancel();
            }
        });
    }

}